package UIFree.controls.popupMenu {
	import SEApp.se_util;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_button;
	import UIFree.controls.uif_gbutton;
	import UIFree.controls.uif_popupMenu;
	import UIFree.uif_component;
	
	import UIUtils.uif_util;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class uif_submenuButton extends uif_gbutton {
		private var _menu		: uif_popupMenu;
		private var _selectFunc	: Function;
		public function uif_submenuButton(parent		:uif_component, 
										  ax			:int, 
										  ay			:int,
										  a_width		: int, 
										  a_height		: int,
										  caption		: String,
										  a_styleClass	: String,
										  menuList		: Array = null, // [{caption: String, event/method: String}]
										  defaultIndex	: int	= 0, 
										  selectFunc	: Function=null,
											submenuOptions	: Object=null) {
			super(parent, ax, ay, a_width, a_height, a_styleClass);
			
			_selectFunc = selectFunc;
			text = caption;
			
			if (_selectFunc != null) { 
				addEvent(MouseEvent.CLICK, function(e: Event): void {
					_selectFunc(menuList[defaultIndex]);
				});
			}
			
			if (menuList && (menuList.length > 1)) {
				_menu = new uif_popupMenu(parentFromMenu(), uif_util.union({
					orientation	: uif_popupMenu.BOTTOMPOSITION,
					itemClass	: uif_button,
					showEvent	: MouseEvent.ROLL_OVER
				}, submenuOptions), a_width, 200, style['submenu']);
				
				_menu.listMenu = menuList;
				
				_menu.activeControl = this;
				_menu.addEvent(Event.SELECT, onSelect);
			}
		}
		
		protected function parentFromMenu(): uif_component {
			return parentComponent;
		}
		
		protected function onSelect(e: ObjectEvent): void {
			_selectFunc(e.object);
		}
		
		override public function dispose():void {
			_menu.dispose();
			super.dispose();
		} 
		
	}
}